/**
 * 
 */
package persistence;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import jpa_objects.Business;
import jpa_objects.City;
import jpa_objects.Event;
import jpa_objects.Instructor;
import jpa_objects.Message;
import jpa_objects.Player;
import jpa_objects.User;
import json_objects_send.Basic_User;
import json_objects_send.Event_Info;
import json_objects_send.JSON_review;
import logic.RSA;
import logic.Sorter;


import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;



/**
 * @author Alejandro Casas
 *
 */
public class DBCommonConsult {



	public static final String CONVERSATIONS = "conversations";
	public static final String LAZY = "lazy";
	public static final String REVIEWS = "reviews";
	public static final String FOLLOW_FRIENDS = "follow_friends";
	public static final String FULL = "full";
	public static final String EVENTS = "events";


	public DBCommonConsult() {

	}


	public List<jpa_objects.Sport> getSports() {
		Session session=null;
		if(DBConnector.sf!=null){
			session = DBConnector.sf.getCurrentSession();

		}
		if(session==null||!session.isOpen())
		{
			session =DBConnector.openSession();
		}
		session.beginTransaction();


		Criteria crit = session.createCriteria(jpa_objects.Sport.class);

		@SuppressWarnings("unchecked")
		List<jpa_objects.Sport> cats = crit.list();
		session.getTransaction().commit();
		return cats;
	}


	public List<City> getCities(String country) {
		Session session=null;
		if(DBConnector.sf!=null){
			session = DBConnector.sf.getCurrentSession();

		}
		if(session==null||!session.isOpen())
		{
			session =DBConnector.openSession();
		}
		session.beginTransaction();


		Criteria crit = session.createCriteria(jpa_objects.City.class);
		crit.add(Restrictions.eq("country", country));

		@SuppressWarnings("unchecked")
		List<jpa_objects.City> cats = crit.list();
		session.getTransaction().commit();
		return cats;
	}


	public jpa_objects.Event getEvent(long id) {
		Session session=null;
		if(DBConnector.sf!=null){
			session = DBConnector.sf.getCurrentSession();

		}
		if(session==null||!session.isOpen())
		{
			session =DBConnector.openSession();
		}
		session.beginTransaction();
		Event u=(Event)session.get(Event.class, id);
		session.getTransaction().commit();
		return u;
	}


	public Player getPlayer(String mail) {
		System.out.println("XXXXXXXXXXXXXXXXXXX");
		System.out.println(mail);
		System.out.println("XXXXXXXXXXXXXXXXXXX");
		Session session=null;
		if(DBConnector.sf!=null){
			session = DBConnector.sf.getCurrentSession();

		}
		if(session==null||!session.isOpen())
		{
			session =DBConnector.openSession();
		}
		session.beginTransaction();
		User u=(User)session.get(User.class, mail);

		if(u!=null)
		{
			u.getEvents().size();
			u.getFollowing_businesses().size();
			u.getFollowing_instructors().size();
			if(u instanceof Player)
			{
				((Player) u).getFriends().size();
			}
		}
		session.getTransaction().commit();
		if(u instanceof Player)
		{
			return (Player)u;
		}
		return new Player("", "", "", "", "", "", "", 0, "", "", "", 0, "", false, "", "", false, "", null,"");

	}

	public Instructor getInstructor(String mail)
	{
		Session session=null;
		if(DBConnector.sf!=null){
			session = DBConnector.sf.getCurrentSession();

		}
		if(session==null||!session.isOpen())
		{
			session =DBConnector.openSession();
		}
		session.beginTransaction();
		Instructor ins=(Instructor)session.get(Instructor.class, mail);
		session.getTransaction().commit();
		return ins;
	}

	public User getUser(String mail,String param) {
		Session session=null;
		if(DBConnector.sf!=null){
			session = DBConnector.sf.getCurrentSession();

		}
		if(session==null||!session.isOpen())
		{
			session =DBConnector.openSession();
		}
		session.beginTransaction();
		User u=(User)session.get(User.class, mail);
		if (param.equals(DBCommonConsult.CONVERSATIONS)) {
			u.getSent().size();
			u.getReceived().size();
		}
		else if(param.equals(DBCommonConsult.FOLLOW_FRIENDS))
		{
			u.getFollowing_businesses().size();
			u.getFollowing_instructors().size();
			if(u instanceof Business )
			{
				((Business)u).getFollowers().size();

			}
			else if(u instanceof Instructor)
			{
				((Instructor)u).getFollowers().size();
			}
			else if(u instanceof Player)
			{
				((Player) u).getFriends().size();
			}
		}
		else if(param.equals(DBCommonConsult.REVIEWS))
		{
			if(u instanceof Business  )
			{
				((Business) u).getReviews().size();

			}
			else if( u instanceof Instructor)
			{
				((Instructor) u).getReviews().size();
			}
			else if(u instanceof Player)
			{
				/*Review
				 * 
				Query searchp =session.getNamedQuery("searchReviews");

				searchp.setParameter("sport", sport);
				searchp.setParameter("city", city);
				searchp.setParameter("country", country);
				 */
				((Player) u).getReviews().size();
			}
		}
		else if(param.equals(DBCommonConsult.EVENTS))
		{
			u.getEvents().size();
		}
		else if(param.equals(DBCommonConsult.FULL))
		{
			u.getEvents().size();
			u.getFollowing_businesses().size();
			u.getFollowing_instructors().size();
			if(u instanceof Business  )
			{
				((Business) u).getReviews().size();

			}
			else if( u instanceof Instructor)
			{
				((Instructor) u).getReviews().size();
			}
			else if(u instanceof Player)
			{
				/*
				Query searchp =session.getNamedQuery("searchReviews");

				searchp.setParameter("sport", sport);
				searchp.setParameter("city", city);
				searchp.setParameter("country", country);
				 */
				((Player) u).getReviews().size();
			}
		}
		session.getTransaction().commit();
		return u;
	}


	public List<Message> getUserMessagesWith(String mailUser, String mailWith) {
		Session session=null;
		if(DBConnector.sf!=null){
			session = DBConnector.sf.getCurrentSession();

		}
		if(session==null||!session.isOpen())
		{
			session =DBConnector.openSession();
		}
		session.beginTransaction();


		Criteria crit = session.createCriteria(jpa_objects.Message.class);
		crit.add(Restrictions.eq("mailFrom", mailUser));
		crit.add(Restrictions.eq("mailTo", mailWith));

		@SuppressWarnings("unchecked")
		List<jpa_objects.Message> cats = crit.list();
		Criteria crit2 = session.createCriteria(jpa_objects.Message.class);
		crit.add(Restrictions.eq("mailFrom", mailWith));
		crit.add(Restrictions.eq("mailTo", mailUser));

		@SuppressWarnings("unchecked")
		List<jpa_objects.Message> cats2 = crit2.list();
		cats.addAll(cats2);
		Sorter.quickSort(cats, 0, cats.size());
		session.getTransaction().commit();
		return cats;
	}


	public User verifyUser(String mail, String password) {
		Session session=null;
		if(DBConnector.sf!=null){
			session = DBConnector.sf.getCurrentSession();

		}
		if(session==null||!session.isOpen())
		{
			session =DBConnector.openSession();
		}
		session.beginTransaction();

		Criteria crit = session.createCriteria(User.class);
		crit.add(Restrictions.eq("mail", mail));
		try {
			String passw=RSA.encryptString(password);
			//String passw=password;
			System.out.println(passw);
			crit.add(Restrictions.eq("password",passw ));

			List<User> cats = crit.list();
			if(cats.size()==1)
			{
				return cats.get(0);
			}
			else
			{
				return null;
			}
		}
		catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}


	public Business getBusiness(String mail) {
		Session session=null;
		if(DBConnector.sf!=null){
			session = DBConnector.sf.getCurrentSession();

		}
		if(session==null||!session.isOpen())
		{
			session =DBConnector.openSession();
		}
		session.beginTransaction();
		Business u=(Business)session.get(Business.class, mail);
		session.getTransaction().commit();
		return u;
	}


	public List<Player> getPlayers(String mail, String sport, String city,
			String country, boolean privacy) {

		Session session=null;
		if(DBConnector.sf!=null){
			session = DBConnector.sf.getCurrentSession();

		}
		if(session==null||!session.isOpen())
		{
			session =DBConnector.openSession();
		}
		session.beginTransaction();

		Query searchp =session.getNamedQuery("searchPlayers");

		searchp.setParameter("sport", sport);
		searchp.setParameter("city", city);
		searchp.setParameter("country", country);
		List<Player> l=searchp.list();
		for (Player p : l) {
			p.getSport_califications().size();
		}
		session.getTransaction().commit();
		return l;

	}


	public List<Business> getBusiness(String mail, String city, String country,
			boolean privacy,String sport,String type) {
		Session session=null;
		if(DBConnector.sf!=null){
			session = DBConnector.sf.getCurrentSession();

		}
		if(session==null||!session.isOpen())
		{
			session =DBConnector.openSession();
		}
		session.beginTransaction();

		Query searchp =session.getNamedQuery("searchBusiness");

		searchp.setParameter("sport", sport);
		searchp.setParameter("city", city);
		searchp.setParameter("country", country);
		searchp.setParameter("type", type);
		session.getTransaction().commit();
		return searchp.list();
	}


	public List<Instructor> getCoaches(String mail, String sport, String city,
			String country, boolean privacy) {
		Session session=null;
		if(DBConnector.sf!=null){
			session = DBConnector.sf.getCurrentSession();

		}
		if(session==null||!session.isOpen())
		{
			session =DBConnector.openSession();
		}
		session.beginTransaction();

		Query searchp =session.getNamedQuery("searchCoaches");

		searchp.setParameter("sport", sport);
		searchp.setParameter("city", city);
		searchp.setParameter("country", country);
		//session.getTransaction().commit();
		return searchp.list();
	}


	public List<Event> getEvents(String mail, String sport, String city,
			String country, boolean privacy) {
		Session session=null;
		if(DBConnector.sf!=null){
			session = DBConnector.sf.getCurrentSession();

		}
		if(session==null||!session.isOpen())
		{
			session =DBConnector.openSession();
		}
		session.beginTransaction();

		Query searchp =session.getNamedQuery("searchEvents");

		searchp.setParameter("sport", sport);
		searchp.setParameter("city", city);
		searchp.setParameter("country", country);
		session.getTransaction().commit();
		return searchp.list();
	}


	public List<User> getUsers(String string) {
		Session session=null;
		if(DBConnector.sf!=null){
			session = DBConnector.sf.getCurrentSession();

		}
		if(session==null||!session.isOpen())
		{
			session =DBConnector.openSession();
		}
		session.beginTransaction();
		Query searchUsers =session.getNamedQuery("searchUsers");
		string="%"+string+"%";
		searchUsers.setParameter("string", string);

		List<User> usrs = searchUsers.list();
		session.getTransaction().commit();
		return usrs;
	}


	public List<Player> getPlayersWraper(String mail, String wraper,
			String city, String country, boolean privacy) {

		Session session=null;
		if(DBConnector.sf!=null){
			session = DBConnector.sf.getCurrentSession();

		}
		if(session==null||!session.isOpen())
		{
			session =DBConnector.openSession();
		}
		session.beginTransaction();

		Query searchp =session.getNamedQuery("searchPlayersWraper");

		searchp.setParameter("wraper", wraper);
		searchp.setParameter("city", city);
		searchp.setParameter("country", country);
		List<Player> l=searchp.list();
		for (Player p : l) {
			p.getSport_califications().size();
		}
		session.getTransaction().commit();
		return l;

	}


	public List<Business> getBusinessWraper(String mail, String city,
			String country, boolean privacy, String wraper,String type) {
		Session session=null;
		if(DBConnector.sf!=null){
			session = DBConnector.sf.getCurrentSession();

		}
		if(session==null||!session.isOpen())
		{
			session =DBConnector.openSession();
		}
		session.beginTransaction();

		Query searchp =session.getNamedQuery("searchBusinessWrap");

		searchp.setParameter("wraper", wraper);
		searchp.setParameter("city", city);
		searchp.setParameter("country", country);
		searchp.setParameter("type", type);
		session.getTransaction().commit();
		return searchp.list();
	}




}
